<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of task_phorum_obj
 *
 * @author Juraj
 */
class task_phorum_obj {
    
    public function addPost($in_data){
        if (empty($in_data)){
            return new error_obj('addPost -> empty $in_data', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $result = $db_obj->insert($GLOBALS['tb_task_phorum'], $in_data);        
        return $result;
    }
    
    public function editPost($in_data, $keys) {
        if (empty($in_data)){
            return new error_obj('editPost -> empty $in_data', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        $result = $db_obj->update($GLOBALS['tb_task_phorum'], $in_data, $keys);
        return $result;
    }
    
    public function deletePostById($id) {
        $id = (int) $id;
        if (empty($id)){
            return new error_obj('deletePost -> empty $id', get_class(), __FUNCTION__);
        }
        $db_obj = new db_obj();
        
        $sql = "DELETE FROM " . $GLOBALS['tb_task_phorum'] . " WHERE task_phorum_id='".$id."'";
        
        $result = $db_obj->query($sql);
        return $result;
    }
    
    public function getPostsInPhorum($filter = array(), $order = '', $limit = '', $cols = array()){
        $db_obj = new db_obj();
        if ($order != '')
            $order = ' ORDER BY ' . $order;
        if ($limit != '')
            $limit = ' LIMIT ' . $limit;
        
        $where = 'WHERE 1=1';
        
        foreach ($filter as $key => $value) {
            $where .= " AND ".$key."='".$value."'";
        }
        
        $what = '';
        if (!empty($cols)){
            foreach ($cols as $value) {
                $what .= $value . ',';
            }
            $what = substr($what, 0, strlen($what)-1);
        } else {
            $what .= '*';
        }
        
        $sql = "SELECT $what FROM " . $GLOBALS['tb_task_phorum'] . " $where $order $limit";
        
        return $db_obj->getAll($sql);
    }
    
    public function getPostById($id) {
        $id = (int) $id;
        
        if (empty($id))
            return new error_obj ('empty id', __CLASS__, __FUNCTION__);
        
        $db_obj = new db_obj();
        $sql = "SELECT * FROM " .$GLOBALS['tb_task_phorum']. " WHERE task_phorum_id='".$id."'";
        
        return $db_obj->getRow($sql);
    }
    
}

?>
